package com.daffodil.flowable.entity;

import java.util.Date;
import java.util.ListIterator;

import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.task.Comment;

import com.daffodil.core.entity.BaseEntity;
import com.daffodil.util.StringUtils;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 历史环节信息
 * @author yweijian
 * @date 2021年11月16日
 * @version 1.0
 * @description
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ActCnHistoricActivity extends BaseEntity<String>{
    
    private static final long serialVersionUID = -7982947649140462503L;

    private String id;
    
    private String activityId;

    /** 环节名称 */
    private String activityName;

    private String activityType;

    private String processInstanceId;

    private String executionId;

    private String taskId;

    private String tenantId;

    /** 执行人 */
    private String assignee;

    private Date startTime;

    private Date endTime;

    private String deleteReason;
    
    /** 办理意见 */
    private String comment;

    public ActCnHistoricActivity() {
        super();
    }

    public ActCnHistoricActivity(HistoricActivityInstance hai, ListIterator<Comment> it){
        this.setId(hai.getId());
        activityId = hai.getActivityId();
        activityName = hai.getActivityName();
        activityType = hai.getActivityType();
        if("startEvent".equals(activityType)) {//开始节点
            activityName = StringUtils.isEmpty(activityName) ? "开始" : activityName;
        }
        if("endEvent".equals(activityType)) {//结束节点
            activityName = StringUtils.isEmpty(activityName) ? "结束" : activityName;
        }
        if("userTask".equals(activityType)) {//用户任务节点
            activityName = StringUtils.isEmpty(activityName) ? "用户任务" : activityName;
        }
        processInstanceId = hai.getProcessInstanceId();
        taskId = hai.getTaskId();
        executionId = hai.getExecutionId();
        tenantId = hai.getTenantId();
        assignee = hai.getAssignee();
        startTime = hai.getStartTime();
        endTime = hai.getEndTime();
        deleteReason = hai.getDeleteReason();
        if(it != null) {
            while (it.hasNext()) {
                comment += it.next().getFullMessage();
                comment = comment.replace("null", "");//null字符串的坑
                comment = comment.replace("NULL", "");
            }
        }
    }
}
